package com.pactera.madp.cp.service.sync.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.entity.sync.SyncPostInfoEntity;
import com.pactera.madp.cp.common.constant.Constants;
import com.pactera.madp.cp.common.util.sync.SyncUtils;
import com.pactera.madp.cp.config.SyncConfig;
import com.pactera.madp.cp.context.SyncStateContext;
import com.pactera.madp.cp.mapper.sync.SyncPostInfoMapper;
import com.pactera.madp.cp.service.sync.SyncPostInfoHisService;
import com.pactera.madp.cp.service.sync.SyncPostInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class SyncPostInfoServiceImpl extends ServiceImpl<SyncPostInfoMapper, SyncPostInfoEntity> implements SyncPostInfoService {

    private final SyncPostInfoHisService syncPostInfoHisService;
    private final SyncConfig syncConfig;

    @Override
    public void addYesterdayToHis() {
        log.debug("职务信息：addYesterdayToHis");
        String date = DateUtil.format(new Date(), Constants.DATE_FORMAT);
        syncPostInfoHisService.syncOneDay(date);
    }

    @Override
    public void deleteYesterday() {
        log.debug("职务信息：deleteYesterday");
        this.remove(Wrappers.emptyWrapper());
    }

    @Override
    public void parse() {
        log.debug("职务信息：parse");
        DateTime offsetDay = DateUtil.offsetDay(new Date(), -1);
        String fileDate = DateUtil.format(offsetDay, Constants.DATE_FORMAT);
        String filepath = SyncUtils.buildFilePath(syncConfig.getHrPath(), Constants.FN_POST, fileDate);
        File newestFile = SyncUtils.getNewestFile(filepath);

        List<SyncPostInfoEntity> entityList
                = SyncUtils.parse(newestFile, Constants.SEPARATOR_STX, SyncPostInfoEntity.class, syncConfig.getHrCharset());

        entityList = specialHandle(entityList);

        this.saveBatch(entityList);
    }

    @Override
    public void deleteHisOneDay() {
        log.debug("职务信息：deleteHisOneDay");
        DateTime offsetDay = DateUtil.offsetDay(new Date(), -30);
        String date = DateUtil.format(offsetDay, Constants.DATE_FORMAT);
        syncPostInfoHisService.deleteOneDate(date);
    }

    @Override
    public void syncBusiness() {
        log.debug("职务信息：syncBusiness");
        SyncStateContext.get().setFinalState(true);
    }

    // 过滤掉脏数据
    private List<SyncPostInfoEntity> specialHandle(List<SyncPostInfoEntity> postList) {
        List<SyncPostInfoEntity> newList = new ArrayList<>();
        postList.forEach(p -> {
            if (StrUtil.isNotEmpty(p.getId()) && p.getId().length() == 32) {
                newList.add(p);
                return;
            }
            log.error("职务信息脏数据：" + JSONUtil.toJsonStr(p));
        });
        return newList;
    }

}